/**
 * 
 */
package slhs.tempo.scriptGenerator;

import java.awt.Color;
import java.awt.GraphicsEnvironment;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.mlc.swing.layout.LayoutConstraintsManager;

/**
 * WordFeedbackPanel.java
 * 
 * @author Swapnil Devikar
 * 
 */
public class WordFeedbackPanel extends JPanel implements PanelHandler {

    private String[] fontSizes = { "8", "9", "10", "11", "12", "14", "16", "18", "20", "22", "24", "26", "28", "36",
	    "48", "72" };

    private static final String WORD_FEEDBACK_OPTIONS_PANEL_LAYOUT = "WORD_FEEDBACK_OPTIONS_PANEL_LAYOUT.XML";

    private static final Object WORD_FEEDBACK = "1";

    private static WordFeedbackPanel wordFeedbackPanel = new WordFeedbackPanel();

    private JPanel wordFeedbackOptionsPanel;

    private JLabel correctAnswerWordFeedbackLabel;
    private JLabel wrongAnswerWordFeedbackLabel;
    private JLabel feedbackDurationLabel;
    private JLabel correctFontTypeLabel;
    private JLabel correctFontSizeLabel;
    private JLabel correctForegroundColorLabel;
    private JLabel correctBackgroundColorLabel;
    private JLabel wrongFontTypeLabel;
    private JLabel wrongFontSizeLabel;
    private JLabel wrongAnswerFontForegroundColorLabel;
    private JLabel wrongAnswerFontBackgroundColorLabel;

    private JButton wrongAnswerBackgroundChooseColorButton;
    private JButton wrongAnswerForegroundChooseColorButton;
    private JButton correctAnswerForegroundChooseColorButton;
    private JButton correctAnswerBackgroundChooseColorButton;

    private JCheckBox correctFontTypeBoldCheckBox;
    private JCheckBox correctFontTypeItalicCheckBox;
    private JCheckBox wrongFontTypeBoldCheckBox;
    private JCheckBox wrongFontTypeItalicCheckBox;

    private JTextField feedbackDurationTextField;
    private JTextField correctAnswerBackgroundColorTextField;
    private JTextField correctAnswerForegroundColorTextField;
    private JTextField wrongAnswerForegroundColorTextField;
    private JTextField wrongAnswerBackgroundColorTextField;
    private JTextField correctAnswerWordFeedbackTextField;
    private JTextField wrongAnswerWordFeedbackTextField;

    private JComboBox correctFontTypeComboBox;
    private JComboBox correctFontSizeComboBox;
    private JComboBox wrongFontTypeComboBox;
    private JComboBox wrongFontSizeComboBox;

    private WordFeedbackPanel() {
	initComponents();
	addComponents();
    }

    /**
     * 
     */
    private void initComponents() {
	GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
	String[] allFonts = ge.getAvailableFontFamilyNames();

	wordFeedbackOptionsPanel = new JPanel();
	wrongAnswerWordFeedbackTextField = new JTextField("WRONG");
	wrongFontTypeBoldCheckBox = new JCheckBox("Bold");
	wrongFontTypeItalicCheckBox = new JCheckBox("Italic");
	correctForegroundColorLabel = new JLabel("Correct Answer Word Foreground Color");
	wrongFontSizeLabel = new JLabel("Wrong Answer Word Feedback Font Size");
	wrongAnswerForegroundColorTextField = new JTextField();
	feedbackDurationTextField = new JTextField("1");
	wrongAnswerForegroundChooseColorButton = new JButton("Choose");
	correctFontTypeComboBox = new JComboBox(allFonts);
	wrongAnswerFontForegroundColorLabel = new JLabel("Wrong Answer Word Feedback Font Foreground Color");
	correctFontTypeLabel = new JLabel("Correct Answer Feedback Word Font");
	correctFontTypeBoldCheckBox = new JCheckBox("Bold");
	correctAnswerBackgroundChooseColorButton = new JButton("Choose");
	wrongFontTypeComboBox = new JComboBox(allFonts);
	wrongAnswerBackgroundColorTextField = new JTextField();
	wrongAnswerWordFeedbackLabel = new JLabel("Wrong Answer Feedback");
	correctFontTypeItalicCheckBox = new JCheckBox("Italic");
	correctAnswerBackgroundColorTextField = new JTextField();
	correctFontSizeComboBox = new JComboBox(fontSizes);
	correctFontSizeLabel = new JLabel("Correct Answer Word Font Size");
	correctAnswerWordFeedbackLabel = new JLabel("Correct Answer Feedback Word");
	correctAnswerForegroundColorTextField = new JTextField();
	feedbackDurationLabel = new JLabel("Feedback Duration (sec)");
	correctAnswerWordFeedbackTextField = new JTextField("CORRECT");
	correctAnswerForegroundChooseColorButton = new JButton("Choose");
	wrongFontSizeComboBox = new JComboBox(fontSizes);
	wrongFontTypeLabel = new JLabel("Wrong Answer Feedback Word Font Type");
	correctBackgroundColorLabel = new JLabel("Correct Answer Word Background Color");
	wrongAnswerBackgroundChooseColorButton = new JButton("Choose");
	wrongAnswerFontBackgroundColorLabel = new JLabel("Wrong Answer Word Feedback Font Background Color");

	// configure components
	correctAnswerForegroundColorTextField.setBackground(Color.BLACK);
	correctAnswerForegroundColorTextField.setEnabled(false);

	correctAnswerBackgroundColorTextField.setBackground(Color.WHITE);
	correctAnswerBackgroundColorTextField.setEnabled(false);

	wrongAnswerForegroundColorTextField.setBackground(Color.BLACK);
	wrongAnswerForegroundColorTextField.setEnabled(false);

	wrongAnswerBackgroundColorTextField.setBackground(Color.WHITE);
	wrongAnswerBackgroundColorTextField.setEnabled(false);

	correctAnswerForegroundChooseColorButton.addActionListener(this);
	correctAnswerBackgroundChooseColorButton.addActionListener(this);

	wrongAnswerForegroundChooseColorButton.addActionListener(this);
	wrongAnswerBackgroundChooseColorButton.addActionListener(this);

    }

    /**
     * 
     */
    private void addComponents() {
	// here we add the controls to the container.
	LayoutConstraintsManager layoutConstraintsManager = LayoutConstraintsManager.getLayoutConstraintsManager(this
		.getClass().getResourceAsStream(WORD_FEEDBACK_OPTIONS_PANEL_LAYOUT));
	LayoutManager panelLayout = layoutConstraintsManager.createLayout("panel", this);
	wordFeedbackOptionsPanel.setLayout(panelLayout);

	wordFeedbackOptionsPanel.add(wrongAnswerWordFeedbackTextField, "wrongAnswerWordFeedbackTextField");
	wordFeedbackOptionsPanel.add(wrongFontTypeBoldCheckBox, "wrongFontTypeBoldCheckBox");
	wordFeedbackOptionsPanel.add(wrongFontTypeItalicCheckBox, "wrongFontTypeItalicCheckBox");
	wordFeedbackOptionsPanel.add(correctForegroundColorLabel, "correctForegroundColorLabel");
	wordFeedbackOptionsPanel.add(wrongFontSizeLabel, "wrongFontSizeLabel");
	wordFeedbackOptionsPanel.add(wrongAnswerForegroundColorTextField, "wrongAnswerForegroundColorTextField");
	wordFeedbackOptionsPanel.add(feedbackDurationTextField, "feedbackDurationTextField");
	wordFeedbackOptionsPanel.add(wrongAnswerForegroundChooseColorButton, "wrongAnswerForegroundChooseColorButton");
	wordFeedbackOptionsPanel.add(correctFontTypeComboBox, "correctFontTypeComboBox");
	wordFeedbackOptionsPanel.add(wrongAnswerFontForegroundColorLabel, "wrongAnswerFontForegroundColorLabel");
	wordFeedbackOptionsPanel.add(correctFontTypeLabel, "correctFontTypeLabel");
	wordFeedbackOptionsPanel.add(correctFontTypeBoldCheckBox, "correctFontTypeBoldCheckBox");
	wordFeedbackOptionsPanel.add(correctAnswerBackgroundChooseColorButton,
		"correctAnswerBackgroundChooseColorButton");
	wordFeedbackOptionsPanel.add(wrongFontTypeComboBox, "wrongFontTypeComboBox");
	wordFeedbackOptionsPanel.add(wrongAnswerBackgroundColorTextField, "wrongAnswerBackgroundColorTextField");
	wordFeedbackOptionsPanel.add(wrongAnswerWordFeedbackLabel, "wrongAnswerWordFeedbackLabel");
	wordFeedbackOptionsPanel.add(correctFontTypeItalicCheckBox, "correctFontTypeItalicCheckBox");
	wordFeedbackOptionsPanel.add(correctAnswerBackgroundColorTextField, "correctAnswerBackgroundColorTextField");
	wordFeedbackOptionsPanel.add(correctFontSizeComboBox, "correctFontSizeComboBox");
	wordFeedbackOptionsPanel.add(correctFontSizeLabel, "correctFontSizeLabel");
	wordFeedbackOptionsPanel.add(correctAnswerWordFeedbackLabel, "correctAnswerWordFeedbackLabel");
	wordFeedbackOptionsPanel.add(correctAnswerForegroundColorTextField, "correctAnswerForegroundColorTextField");
	wordFeedbackOptionsPanel.add(feedbackDurationLabel, "feedbackDurationLabel");
	wordFeedbackOptionsPanel.add(correctAnswerWordFeedbackTextField, "correctAnswerWordFeedbackTextField");
	wordFeedbackOptionsPanel.add(correctAnswerForegroundChooseColorButton,
		"correctAnswerForegroundChoseColorButton");
	wordFeedbackOptionsPanel.add(wrongFontSizeComboBox, "wrongFontSizeComboBox");
	wordFeedbackOptionsPanel.add(wrongFontTypeLabel, "wrongFontTypeLabel");
	wordFeedbackOptionsPanel.add(correctBackgroundColorLabel, "correctBackgroundColorLabel");
	wordFeedbackOptionsPanel.add(wrongAnswerBackgroundChooseColorButton, "wrongAnswerBackgroundChooseColorButton");
	wordFeedbackOptionsPanel.add(wrongAnswerFontBackgroundColorLabel, "wrongAnswerFontBackgroundColorLabel");
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
     */
    @Override
    public void actionPerformed(ActionEvent event) {
	Object source = event.getSource();
	Color color = JColorChooser.showDialog(this, "Choose Foreground Color", Color.WHITE);

	if (source.equals(correctAnswerForegroundChooseColorButton)) {
	    correctAnswerForegroundColorTextField.setBackground(color);
	} else if (source.equals(correctAnswerBackgroundChooseColorButton)) {
	    correctAnswerBackgroundColorTextField.setBackground(color);
	} else if (source.equals(wrongAnswerForegroundChooseColorButton)) {
	    wrongAnswerBackgroundColorTextField.setBackground(color);
	} else if (source.equals(wrongAnswerBackgroundChooseColorButton)) {
	    wrongAnswerBackgroundColorTextField.setBackground(color);
	}
    }

    /*
     * (non-Javadoc)
     * 
     * @see slhs.tempo.scriptGenerator.PanelHandler#getPanel()
     */
    @Override
    public JPanel getPanel() {
	// TODO Auto-generated method stub
	return wordFeedbackOptionsPanel;
    }

    /**
     * @return
     */
    public static WordFeedbackPanel getInstance() {
	return wordFeedbackPanel;
    }

    public String getFeedbackString() {
	StringBuffer wordFeedbackString = new StringBuffer();
	// 1 for word feedback
	wordFeedbackString.append(WORD_FEEDBACK);
	wordFeedbackString.append(UtilityConstants.VALUES_DELIMITER);

	// correct answer word feedback
	wordFeedbackString.append(getCorrectAnswerWordFeedback());
	wordFeedbackString.append(UtilityConstants.VALUES_DELIMITER);

	// wrong answer word feedback
	wordFeedbackString.append(getWrongAnswerWordFeedback());
	wordFeedbackString.append(UtilityConstants.VALUES_DELIMITER);

	// feedback duration
	wordFeedbackString.append(getFeedbackDuration());
	wordFeedbackString.append(UtilityConstants.VALUES_DELIMITER);

	// font type for correct word feedback
	wordFeedbackString.append(getCorrectFontType());
	wordFeedbackString.append(UtilityConstants.VALUES_DELIMITER);

	// font size for correct word feedback
	wordFeedbackString.append(getCorrectFontSize());
	wordFeedbackString.append(UtilityConstants.VALUES_DELIMITER);

	// True is bold
	wordFeedbackString.append(isCorrectFontTypeBold());
	wordFeedbackString.append(UtilityConstants.VALUES_DELIMITER);

	// True if italic
	wordFeedbackString.append(isCorrectFontTypeItalic());
	wordFeedbackString.append(UtilityConstants.VALUES_DELIMITER);

	// foreground color for correct word
	wordFeedbackString.append(getCorrectFontForegroundColor());
	wordFeedbackString.append(UtilityConstants.VALUES_DELIMITER);

	// background color for correct word
	wordFeedbackString.append(getCorrectFontBackgroundColor());
	wordFeedbackString.append(UtilityConstants.VALUES_DELIMITER);

	// font type for wrong word feedback
	wordFeedbackString.append(getWrongFontType());
	wordFeedbackString.append(UtilityConstants.VALUES_DELIMITER);

	// font size for wrong word feedback
	wordFeedbackString.append(getWrongFontSize());
	wordFeedbackString.append(UtilityConstants.VALUES_DELIMITER);

	// True if bold
	wordFeedbackString.append(isWrongFontTypeBold());
	wordFeedbackString.append(UtilityConstants.VALUES_DELIMITER);

	// True if italic
	wordFeedbackString.append(isWrongFontTypeItalic());
	wordFeedbackString.append(UtilityConstants.VALUES_DELIMITER);

	// foreground color for correct word
	wordFeedbackString.append(getWrongFontForegroundColor());
	wordFeedbackString.append(UtilityConstants.VALUES_DELIMITER);

	// background color for correct word
	wordFeedbackString.append(getWrongFontBackgroundColor());

	return wordFeedbackString.toString();
    }

    /**
     * @return
     */
    private int getWrongFontForegroundColor() {
	Color foregroudColor = wrongAnswerForegroundColorTextField.getBackground();
	String rgb = Integer.toHexString(foregroudColor.getRGB());
	rgb = rgb.substring(2, rgb.length());
	return Integer.parseInt(rgb, 16);
    }

    /**
     * @return
     */
    private Object isWrongFontTypeItalic() {
	boolean isItalic = wrongFontTypeItalicCheckBox.isSelected();
	return ((isItalic) ? UtilityConstants.TRUE : UtilityConstants.FALSE);
    }

    /**
     * @return
     */
    private String isWrongFontTypeBold() {
	boolean isBold = wrongFontTypeBoldCheckBox.isSelected();
	return ((isBold) ? UtilityConstants.TRUE : UtilityConstants.FALSE);
    }

    /**
     * @return
     */
    private int getWrongFontBackgroundColor() {
	Color backgroundColor = wrongAnswerBackgroundColorTextField.getBackground();
	String rgb = Integer.toHexString(backgroundColor.getRGB());
	rgb = rgb.substring(2, rgb.length());
	return Integer.parseInt(rgb, 16);
    }

    /**
     * @return
     */
    private int getCorrectFontBackgroundColor() {
	Color backgroundColor = correctAnswerBackgroundColorTextField.getBackground();
	String rgb = Integer.toHexString(backgroundColor.getRGB());
	rgb = rgb.substring(2, rgb.length());
	return Integer.parseInt(rgb, 16);
    }

    /**
     * @return
     */
    private String getWrongFontSize() {

	return wrongFontSizeComboBox.getSelectedItem().toString();
    }

    /**
     * @return
     */
    private String getWrongFontType() {
	return wrongFontTypeComboBox.getSelectedItem().toString();
    }

    /**
     * @return
     */
    private int getCorrectFontForegroundColor() {
	Color foregroudColor = correctAnswerForegroundColorTextField.getBackground();
	String rgb = Integer.toHexString(foregroudColor.getRGB());
	rgb = rgb.substring(2, rgb.length());
	return Integer.parseInt(rgb, 16);
    }

    /**
     * @return
     */
    private String isCorrectFontTypeItalic() {
	boolean isItalic = correctFontTypeItalicCheckBox.isSelected();
	return ((isItalic) ? UtilityConstants.TRUE : UtilityConstants.FALSE);
    }

    /**
     * @return
     */
    private String isCorrectFontTypeBold() {
	boolean isBold = correctFontTypeBoldCheckBox.isSelected();
	return ((isBold) ? UtilityConstants.TRUE : UtilityConstants.FALSE);
    }

    /**
     * @return
     */
    private String getCorrectFontSize() {
	return correctFontSizeComboBox.getSelectedItem().toString();
    }

    /**
     * @return
     */
    private String getCorrectFontType() {

	return correctFontTypeComboBox.getSelectedItem().toString();
    }

    /**
     * @return
     */
    private String getFeedbackDuration() {
	return feedbackDurationTextField.getText().toString();
    }

    private String getCorrectAnswerWordFeedback() {
	return correctAnswerWordFeedbackTextField.getText().toString();
    }

    private String getWrongAnswerWordFeedback() {
	return wrongAnswerWordFeedbackTextField.getText().toString();
    }
}
